home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Toolbox / Menu Defproc 1.0.3 / SizeTkl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-25  |  6.2 KB  |  174 lines  |  [TEXT/MPS ]

  1. /*============================================================================*\
  2. * SizeTkl.c - mSizeMsg processor
  3. *
  4. * SizeTkl.c contains the code to calculate the size of a pull-down menu.  Also,
  5. * services are provided through CalItemHeight and CalcItemWidth to calculate the
  6. * height and width of a single menu item.
  7. \*============================================================================*/
  8.  
  9.  
  10. /******************************************************************************\
  11. * Header Files
  12. \******************************************************************************/
  13.  
  14. #include <Memory.h>
  15. #include <Quickdraw.h>
  16. #include <Script.h>
  17. #include <Types.h>
  18. #include "Concordia.h"
  19. #include "DrawTkl.h"
  20. #include "SizeTkl.h"
  21.  
  22.  
  23. #pragma segment Main
  24. /******************************************************************************\
  25. * DoSizeMsg - Calculate the horizontal and vertical size of menu in pixels
  26. *
  27. * DoSizeMsg calculates the horizontal and vertical size in pixels of the menu
  28. * specified specified by TheMenu.  The menuWidth and menuHeight fields are
  29. * filled with this information.
  30. \******************************************************************************/
  31.  
  32. void
  33. DoSizeMsg (TheMenu)
  34.     MenuHandle TheMenu; //=> Menu to size <>
  35.     {
  36.     Str255      *ItemString; //-> Item's string
  37.     ItemInfoPtr ItemInfo;    //-> Item info
  38.     short       TestWidth;   //Width of item to test
  39.     short       Height;      //Height of menu
  40.     short       Width;       //Width of menu
  41.     short       ScrnHeight;  //Screen height in pixels w/o menu bar & margin
  42.  
  43.     Height = Width = 0;
  44.     ScrnHeight = qdGlobPtr->screenBits.bounds.bottom - qdGlobPtr->screenBits.
  45.             bounds.top - GetMBarHeight () - scrnMargin;
  46.     HLock ((Handle) TheMenu);
  47.     ItemString = (Str255 *) (**TheMenu).menuData;
  48.     ItemString = (Str255 *) ((Byte *) ItemString + strSize (*ItemString));
  49.     while ((*ItemString) [0] != (char) 0)
  50.         {
  51.         ItemInfo = (ItemInfoPtr) ((Byte *) *ItemString + strSize (*ItemString));
  52.         Height += CalcItemHeight (*ItemString, ItemInfo);
  53.         TestWidth = CalcItemWidth (*ItemString, ItemInfo);
  54.         if (TestWidth > Width)
  55.             Width = TestWidth;
  56.         ItemString = (Str255 *) (ItemInfo + 1);
  57.         }
  58.     HUnlock ((Handle) TheMenu);
  59.     (**TheMenu).menuWidth = Width;
  60.     if (Height > ScrnHeight)
  61.         (**TheMenu).menuHeight = ScrnHeight;
  62.     else
  63.         (**TheMenu).menuHeight = Height;
  64.     }
  65.  
  66.  
  67. #pragma segment Main
  68. /******************************************************************************\
  69. * CalcItemHeight - Calculate the height of a menu item in pixels
  70. *
  71. * CalcItemHeight calculates the height of the menu item (whose information is
  72. * specified by ItemData) in pixels.  The size of an icon and separation bars
  73. * are accounted for.  The height is returned.  ItemString is the item's string.
  74. * CurrFont is a FontInfo record containing information about the current item's
  75. * font specs, taking the current style into account.
  76. *
  77. * Coding Note:
  78. * #A# - If the first character of the item string is a hyphen, DrawItem (source
  79. *       in DrawTkl.c) will just draw a gray, horizontal line.  The height of any
  80. *       "gray, horizontal line" items will always be stdItemHeight.
  81. * #B# - These command-key equivalents indicate that the item's icon should be
  82. *       scaled to half its size.
  83. \******************************************************************************/
  84.  
  85. short
  86. CalcItemHeight (ItemString, ItemData)
  87.     Str255      ItemString; //Name of menu item >>
  88.     ItemInfoPtr ItemData;   //Information for menu item >>
  89.     {
  90.     short        TotHeight; //Total height of menu item in pixels
  91.     short        Height;    //Height of menu item in pixels
  92.     short        IconSize;  //Size of icon, including vertical margins
  93.     FontInfo     CurrFont;  //Current font's characteristics
  94.     TextStateRec TextState; //Current port's text characteristics
  95.  
  96.     TotHeight = itemVertMarg << 1;
  97.     if (ItemString [1] == '-') //#A#
  98.         TotHeight += stdItemHeight;
  99.     else
  100.         {
  101.         GetTextState (&TextState);
  102.         TextFace (ItemData->charStyle);
  103.         GetFontInfo (&CurrFont);
  104.         Height = CurrFont.ascent + CurrFont.descent + CurrFont.leading;
  105.         if (ItemData->iconNum != (Byte) 0)
  106.             {
  107.             if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
  108.                     (char) 0x1F) //#B#
  109.                 IconSize = (short) (iconSize / 2 + iconVertMarg * 2);
  110.             else
  111.                 IconSize = (short) (iconSize + iconVertMarg * 2);
  112.             if (IconSize > Height)
  113.                 Height = IconSize;
  114.             }
  115.         if (ItemData->kbdEquiv == (char) hMenuCmd)
  116.             if (sicnSize > Height)
  117.                 Height = sicnSize;
  118.         TotHeight += Height;
  119.         SetTextState (&TextState);
  120.         }
  121.     return TotHeight;
  122.     }
  123.  
  124.  
  125. #pragma segment Main
  126. /******************************************************************************\
  127. * CalcItemWidth - Calculate the width of a menu item in pixels
  128. *
  129. * CalcItemWidth calculates the width of the menu item whose item data is
  130. * specified by ItemData.  The item's string is given in ItemString, the specs
  131. * for the current font (with item styles) is given in CurrFont, and the
  132. * maximum width in pixels of the command-key equivalents is given in CmmdWidth.
  133. * The textFace field of the current GrafPort might be changed.
  134. *
  135. * Coding Notes
  136. * #A# - These command-key equivalents indicate that the item's icon should be
  137. *       scaled to half its size.
  138. \******************************************************************************/
  139.  
  140. short
  141. CalcItemWidth (ItemString, ItemData)
  142.     Str255      ItemString; //Name of menu item >>
  143.     ItemInfoPtr ItemData;   //Information for menu item >>
  144.     {
  145.     short        TotWidth;  //Total width of menu item in pixels (except mark)
  146.     short        Width;     //Height of menu item in pixels
  147.     FontInfo     CurrFont;  //Current font's characteristics
  148.     TextStateRec TextState; //Current port's text characteristics
  149.  
  150.     Width = 0;
  151.     GetFontInfo (&CurrFont);
  152.     TotWidth = (short) (itemHorzMarg << 1) + CurrFont.widMax + (short)
  153.             markItemGap;
  154.     if (ItemString [1] != '-')
  155.         {
  156.         GetTextState (&TextState);
  157.         if (ItemData->kbdEquiv > '!')
  158.             Width = CharWidth (cmmdCharCode) + CurrFont.widMax + cmmdItemGap;
  159.         TextFace (ItemData->charStyle);
  160.         Width += StringWidth (ItemString);
  161.         if (ItemData->iconNum != (Byte) 0)
  162.             if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
  163.                     (char) 0x1F) //#A#
  164.                 Width += (short) (iconSize / 2 + iconItemGap);
  165.             else
  166.                 Width += (short) (iconSize + iconItemGap);
  167.         if (ItemData->kbdEquiv == (char) hMenuCmd)
  168.             Width += sicnSize;
  169.         TotWidth += Width;
  170.         SetTextState (&TextState);
  171.         }
  172.     return TotWidth;
  173.     }
  174.